{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras.preprocessing.image import ImageDataGenerator,array_to_img,img_to_array,load_img"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* rotation_range是一个0~180的度数，用来指定随机选择图片的角度\n",
    "* width_shift和height_shift用来指定水平和竖直方向随机移动的程度，这是两个0~1之间的比\n",
    "* rescale值将在执行其他处理前乘到整个图像上，我们的图像在RGB三个通道都说0-255的整数，这样的操作可能使图像的值过高或过低，所以我们将这个值定为0~1之间的数。\n",
    "* shear_range是用来进行剪切变换的程度，参考剪切变换\n",
    "* zoom_range是用来进行随机的放大\n",
    "* horizontal_filp随机的对图片进行水平翻转，这个参数适用于水平翻转不影响图片语义的时候\n",
    "* fill_mode用来指定当需要进行像素填充，如旋转，水平和竖直位移时，如何填充新出现的像素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "datagen = ImageDataGenerator(\n",
    "             rotation_range = 40,      #随机翻转角度\n",
    "             width_shift_range = 0.2,  #随机水平平移\n",
    "             height_shift_range=0.2,   #随机竖直平移\n",
    "             rescale=1./255,           #数值归一化\n",
    "             shear_range=0.2,          #随机裁剪\n",
    "             zoom_range=0.2,           #随机放大\n",
    "             horizontal_flip=True,     #水平翻转\n",
    "             fill_mode='nearest')      #填充方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(374, 500, 3)\n",
      "(1, 374, 500, 3)\n"
     ]
    }
   ],
   "source": [
    "#载入图片\n",
    "img = load_img('Images/train/cat/cat.0.jpg')\n",
    "x = img_to_array(img)\n",
    "print(x.shape)\n",
    "x = x.reshape((1,) + x.shape)\n",
    "print(x.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 0\n",
    "#生成20张图片\n",
    "#flow随机生成图片\n",
    "for batch in datagen.flow(x,batch_size=1,save_to_dir='temp',save_prefix='cat',save_format='jpeg'):\n",
    "    #执行20次\n",
    "    i += 1\n",
    "    if i > 20:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
